package com.erp.database.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class UUIDUtils {

	/**
	 * 日期格式转成字符串
	 * 
	 * @param date
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static String dateToStr(Date date, String pattern) {
		SimpleDateFormat format = null;
		if (pattern == null || "".equals(pattern)) {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else {
			format = new SimpleDateFormat(pattern);
		}
		if (date == null){
			return "";
		} else{
			return format.format(date);
		}
	}

	private static String dateToStr(String pattern) {
		return dateToStr(Calendar.getInstance().getTime(), pattern);
	}
	/**
	 * 18位UUID
	 * @return
	 */
	public static String shortUUID() {
		String pattern = "yyyyMMdd";
		int hashCodeV = UUID.randomUUID().toString().hashCode();
		String prefix = dateToStr(pattern);
		System.out.println(hashCodeV);
		// 0 代表前面补充0
		// 15 代表长度为15
		// d 代表参数为正数型
		return prefix + String.format("%010d", Math.abs(hashCodeV));
	}
}
